package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.db.dao.LitemallAdMapper;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.domain.LitemallAdExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallAdService {
    @Resource
    private LitemallAdMapper adMapper;

    public List<LitemallAd> queryIndex(int page,int size) {
        LitemallAdExample example = new LitemallAdExample();
        example.or().andPositionEqualTo((byte) 1).andDeletedEqualTo(false).andEnabledEqualTo(true);
        PageHelper.startPage(page, size);
        return adMapper.selectByExample(example);
    }

    /**
     * 根据 name 去
     * @param name      根据 name模糊查询
     * @param content  根据 content模糊查询
     * @param page     页数
     * @param limit    每页个数
     * @param sort    排序规则 只有  "add_time", "id"
     * @param order   ASC  DESC
     * @return
     */
    public List<LitemallAd> querySelective(String name, String content, Integer page, Integer limit, String sort, String order) {
        LitemallAdExample example = new LitemallAdExample();
        LitemallAdExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return adMapper.selectByExampleWithBLOBs(example);
    }

    public int updateById(LitemallAd ad) {
        ad.setUpdateTime(LocalDateTime.now());
        return adMapper.updateByPrimaryKeySelective(ad);
    }

    public void deleteById(Integer id) {
        adMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallAd ad) {
        ad.setAddTime(LocalDateTime.now());
        ad.setUpdateTime(LocalDateTime.now());
        adMapper.insertSelective(ad);
    }

    public LitemallAd findById(Integer id) {
        return adMapper.selectByPrimaryKey(id);
    }

    public LitemallAd findByLink(String link) {
        LitemallAdExample example = new LitemallAdExample();
        example.or().andLinkEqualTo(link).andDeletedEqualTo(false);
        return adMapper.selectOneByExampleWithBLOBs(example);
    }


    /*----------魏来新加 brandId-------------*/

    public PageInfo<LitemallAd> queryByBrandId(String name, Integer page, Integer limit, String sort, String order,Integer brandId) {
        LitemallAdExample example = new LitemallAdExample();
        LitemallAdExample.Criteria c1 = example.createCriteria();
        LitemallAdExample.Criteria c2 = example.createCriteria();
        //名字..去题目和内容中找
        PageHelper.startPage(page, limit);
            if (StringUtils.isNotEmpty(name)) {
                c1.andNameLike("%" + name+ "%").andDeletedEqualTo(false);
            }
            if (StringUtils.isNotEmpty(name)) {
                c2.andContentLike("%" + name + "%").andDeletedEqualTo(false);
            }
            if(null != brandId && brandId > 0){
                c1.andBrandIdEqualTo(brandId);
                c2.andBrandIdEqualTo(brandId);
            }
            if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
                example.setOrderByClause(sort + " " + order);
            }
        PageHelper.startPage(page,limit);
        List<LitemallAd> litemallAds = adMapper.selectByExampleSelective(example);
      return  new PageInfo<>(litemallAds);
    }


//
//    //多条件查询
//    public Page<LitemallBrandPeople> allBrandPeople(Map<String,String> map, Integer page, Integer pageSize){
//        PageHelper.startPage(page,pageSize);
//        LitemallBrandPeopleExample example = new LitemallBrandPeopleExample();
//        LitemallBrandPeopleExample.Criteria criteria = example.createCriteria();
//        if(null != map){
//            if(null!= map.get("brandId")){
//                criteria.andBrandIdEqualTo(Integer.parseInt(map.get("brandId")));
//            }
//            if(null!= map.get("name")){
//                criteria.andNameEqualTo(map.get("name"));
//            }
//            if(null!= map.get("position")){
//                criteria.andPositionEqualTo(map.get("position"));
//            }
//            if(null!= map.get("desc")){
//                criteria.andDescLike("%" + map.get("desc") + "%");
//            }
//        }
//        return (Page)brandPeopleMapper.selectByExampleSelective(example);
//    }
//    //增加
//    public  void addBrandPeople(LitemallBrandPeople brandPeople){
//        brandPeopleMapper.insertSelective(brandPeople);
//    }
//    //删除
//    public  void delBrandPeople(Integer id){
//        brandPeopleMapper.deleteByPrimaryKey(id);
//    }
//    //删除
//    public  void updateBrandPeople(LitemallBrandPeople brandPeople){
//        brandPeopleMapper.updateByPrimaryKeySelective(brandPeople);
//    }
//


}
